

package com.example.app1;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
//import android.content.ContentValues;
//import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
//import android.widget.Toast;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;




public class Data extends Activity {
    private TextView mDateDisplay;
    private Button mPickDate;
    private TextView mTimeDisplay;
    private Button mPickTime;
    private static int mYear;
    private static int mMonth;
    private static int mDay;
    private static int mHour;
    private static int mMinute;

    static final int TIME_DIALOG_ID = 0;
    static final int DATE_DIALOG_ID = -1;
@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.data);
		
		
		
		   // capture our View elements
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);
     // capture our View elements
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mPickTime = (Button) findViewById(R.id.pickTime);

        // add a click listener to the button
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
     // add a click listener to the button
        mPickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

     // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
     // get the current time
        final Calendar b = Calendar.getInstance();
        mHour = b.get(Calendar.HOUR_OF_DAY);
        mMinute = b.get(Calendar.MINUTE);

        // display the current date (this method is below)
        updateDisplay();
        
        // the callback received when the user "sets" the date in the dialog


        
		}
		private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

				public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
					mYear = year;
					mMonth = monthOfYear;
					mDay = dayOfMonth;
					updateDisplay();
				}
		};

		// the callback received when the user "sets" the time in the dialog
		private TimePickerDialog.OnTimeSetListener mTimeSetListener =  new TimePickerDialog.OnTimeSetListener() {
		        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		            mHour = hourOfDay;
		            mMinute = minute;
		            updateDisplay();
		        }
		    };
		    
		
		@Override
		protected Dialog onCreateDialog(int id) {
			switch (id) {
			case DATE_DIALOG_ID:
				return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
			case TIME_DIALOG_ID:
		        return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
			}
			return null;
		}
		
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			super.onCreateOptionsMenu(menu);
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.menu, menu);
			return true;
		}
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
				case R.id.Save_date:
						//startActivity(new Intent(this, Prefs.class));
					//s'ha domplir amb lo que fa el save
						//alta();
						finish();
						return true;
				case R.id.Cancel_date:
					//s/ha domplir amb lo k fa el cancel
						finish();
						return true;
		// More items go here (if any) ...
			}
			return false;
		}

	
		
		
	// updates the date in the TextView
		private void updateDisplay() {
			mDateDisplay.setText(
					new StringBuilder()
                    // Month is 0 based so add 1
                    	.append(mMonth + 1).append("-")
                    	.append(mDay).append("-")
                    	.append(mYear).append(" "));
		
			mTimeDisplay.setText(
					new StringBuilder()
			                .append(pad(mHour)).append(":")
			                .append(pad(mMinute)));
			
		
		
		}
		private static String pad(int c) {
		    if (c >= 10)
		        return String.valueOf(c);
		    else
		        return "0" + String.valueOf(c);
		}
		/*public void alta() {
	        database admin=new database(this, "administracion", null, 1);
	        SQLiteDatabase bd=admin.getWritableDatabase();   
	        ContentValues registro=new ContentValues();
	        registro.put("dia",mDay );
	        registro.put("mes",mMonth );
	        registro.put("any",mYear );
	        registro.put("hora",mHour );
	        registro.put("min",mMinute );
	        bd.insert("votantes", null, registro);
	        bd.close();
	        Toast.makeText(this, "the appointment have been saved", Toast.LENGTH_SHORT).show();        
	    }*/
	
    public static int getYear(){
		return mYear;
    
    }
    public static int getDay(){
		return mDay;
    
    }
    public static int getMonth(){
		return mMonth;
    
    }
    public static int getHour(){
		return mHour;
    }
    public static int getMinute(){
		return mMinute;
    
    }
    
    
}
//http://developer.android.com/guide/topics/appwidgets/index.html




